package com.ct.model.common;

import java.io.Serializable;

/**
 * Created by IntelliJ IDEA.
 * User: nmkravchenko
 * Date: 22.10.2009
 * Time: 18:48:20
 * To change this template use File | Settings | File Templates.
 */
public abstract class BaseEntity<PKType extends Serializable> implements Entity<PKType> {
    public PKType getId() {
        return id;
    }

    protected void setId(PKType aId) {
        id = aId;
    }

    public boolean equals(Object o) {
        if (o == null) return false;
        if (this == o) return true;
        if( isNew() ) return false;
        if ( o.getClass() != getClass()
                && !o.getClass().isAssignableFrom( getClass() )
                && !getClass().isAssignableFrom( o.getClass() ) )
            return false;
        final Entity entity = (Entity) o;

        return id.equals(entity.getId());

    }

    /*public int hashCode() {
        return new Long(id).hashCode();
    }*/

    public int hashCode() {
        return id.hashCode();
    }

    public String toString() {
        return (isNew() ? "id=[none]" : "id=" + id ) + ",hashCode=" + hashCode();
    }

    private PKType id;

    protected BaseEntity() {
    }

    protected BaseEntity(PKType aId) {
        id = aId;
    }
}
